﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SysUserForm.aspx.cs" Inherits="Form_SysUserForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <script src="<%=ResolveClientUrl("~/Resource/js/benik.js")%>" type="text/javascript"></script>
    <title></title>
     <script language="javascript" type="text/javascript">
         $(document).ready(function () {
             $('#panelbar').kendoPanelBar();
             $("#txtName").focus();

             $('#btnClose').click(function () {
                 parent.$('#dialog').dialog('close');
                 return false;
             });

             //var id = $('#<%= hfID.ClientID %>').val();
             //if (id == "") {
                 LoadGroupApp();
             //}

             $('#btnSave').click(function () {
                 if (!$("#form1").valid())
                     return;

                 var name = $('#txtName').val();
                 var email = $('#txtEmail').val();
                 var groupid = $("#ddlGroup option:selected").val();
                 //var groupappid = $("#ddlGroupApp option:selected").val();
                 //alert(parentID);

                 var selectedGroupApp = new Array();
                 $('#tblGroupApp tr').each(function () {
                     var row = $(this);
                     var checkboxID = row.find('input[type="checkbox"]').prop('id');
                     if (row.find('input[type="checkbox"]').is(':checked')) {
                         selectedGroupApp.push(checkboxID);
                     }
                 });


                 var id = $('#<%= hfID.ClientID %>').val();

                 var objUser = new Object();
                 objUser.UserID = id;
                 objUser.Name = encodeURIComponent(name);
                 objUser.Email = encodeURIComponent(email);
                 objUser.GroupID = encodeURIComponent(groupid);
                 objUser.GroupAppID = encodeURIComponent(selectedGroupApp);
                 jsonData = JSON.stringify(objUser);

                 if (id == 0)
                     CallAjax('<%= ResolveUrl("~/handler/sysUserHandler.ashx") %>', "?show=create", jsonData);
                 else
                     CallAjax('<%= ResolveUrl("~/handler/sysUserHandler.ashx") %>', "?show=update", jsonData);

                 parent.$('#dialog').dialog('close');
                 //parent.location.reload();
                 setTimeout(parent.$('#user-grid').data('kendoGrid').dataSource.read(), 3000);
                 setTimeout(parent.$('#user-grid').data('kendoGrid').refresh(), 3000);
                 return false;
             });
         });


         function LoadGroupApp() {
             var handlerUrl = '<%= ResolveUrl("~/Handler/SysApplicationGroupsHandler.ashx") %>';
             var hfGroupAppID = $('#<%= hfGroupAppID.ClientID %>').val();
             //alert(hfGroupAppID);
             $('#tblGroupApp tr').remove();

             $.ajax({
                 type: 'POST',
                 url: handlerUrl + "?type=list",
                 contentType: 'application/json',
                 dataType: 'json',
                 success: function (datas) {
                     $.each(datas, function (i, data) {
                         var check = MappingGroup(data.AppGroupID, data.AppGroupName);

                        if (check) {
                            var html = "<tr><td><input type='checkbox' checked='checked' id='" + data.AppGroupID + "' />" + data.AppGroupName + "</td></tr>";
                                                     
                        }
                        else {
                            var html = "<tr><td><input type='checkbox'  id='" + data.AppGroupID + "' />" + data.AppGroupName + "</td></tr>";
                        }

                       $("#tblGroupApp").append(html)

                     });

                 },
                 error: function () {
                     alert('Error load group app !');
                     //$.msgBox("Error image jabatan !", null, { title: "Pesan Error" });
                 }
             });

         }

         function MappingGroup(code) {
             var hfGroupAppID = $('#<%= hfGroupAppID.ClientID %>').val();
             var substr = hfGroupAppID.split(',');
             var htmledit = "";
             var checkCode = false;
             for (var i = 0; i < substr.length; i++) {

                 if (code == substr[i]) {
                     checkCode = true;
                     break;
                 }

             }

             return checkCode;

         }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    
    
    <div class="grid-box" id="menuform">
        <h3 id="h3Title" runat="server">
        </h3>
        <input type="hidden" name="ID" />
        <ul id="panelbar">
            <li class="k-state-active">Data User
                <div class="forminput">
                    <table width="100%" style="background: #FFF; padding: 10px">
                        <tr>
                            <td>
                                <label for="txtName">
                                    Name</label>
                            </td>
                            <td>
                                <%--<input type="text" class="required" name="Title" size="40" />--%>
                                <asp:TextBox ID="txtName" runat="server" CssClass="required" MaxLength="50" Width="400"> </asp:TextBox>
                            </td>
                        </tr>
                         <tr>
                            <td>
                                <label for="txtEmail">
                                    Email</label>
                            </td>
                            <td>
                                <%--<input type="text" class="required" name="Title" size="40" />--%>
                                <asp:TextBox ID="txtEmail" runat="server" CssClass="required" MaxLength="50" Width="400"> </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td width="150">
                                <label for="txtGroup">
                                    Group</label>
                            </td>
                            <td>
                                <asp:DropDownList ID="ddlGroup" runat="server" CssClass="required"  Width="400" ></asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="txtGroupApp">
                                    Group Application</label>
                            </td>
                            <td>
                                
                                 <table id="tblGroupApp">
                                 </table>
                            </td>
                            
                        </tr>
                       
                    </table>
                </div>
            </li>
        </ul>

         <button id="btnClose" type="button">
                        Close</button>
        <button id="btnSave" type="button">
                        Save</button>
    </div>

    <asp:HiddenField ID="hfID" runat="server" Value="" />
    <asp:HiddenField ID="hfGroupAppID" runat="server" Value="" />
    </form>
</body>
</html>
